<template>
  <el-row>
    <el-col :span="16" :offset="4">
      <el-form  ref="type" :model="typeList" :rules="rules" label-position="right" label-width="100px">
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="typeList.typeName"></el-input>
        </el-form-item >
        <el-form-item label="类型介绍" prop="profile" >
          <el-input v-model="typeList.profile">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button type="warning" @click="onReset">返回</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>

const initType = {
  typeId: '',
  typeName: '',
  profile: ''
}
export default {
  name: 'NewsTypeEdit',
  data () {
    return {
      typeList: { ...initType },
      rules: {
        typeName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }]
        // profile: [{ required: true, message: '请输入类型介绍', trigger: 'blur' }]
      }
    }
  },
  methods: {
    onSubmit () {
      this.$refs.type.validate((valid) => {
        if (valid) {
          this.$axios.post('/sys/type/news/update', this.typeList).then(({ data }) => {
            if (data.code === 200) {
              this.$alert('保存成功').then(() => {
                this.$router.push('newsType')
              })
            }
          })
        } else {
          this.$alert('保存失败')
        }
      })
    },
    onReset () {
      this.typeList = { ...initType }
      this.$router.push('/newsType')
    },
    init () {
      if (this.$route.query.typeId) {
        this.$http.get('/sys/type/info/' + this.$route.query.typeId).then(({ data }) => {
          const type = data.data
          this.typeList = {
            typeId: type.typeId,
            typeName: type.typeName,
            profile: type.profile
          }
        })
      }
    }
  },
  created () {
    this.init()
  }
}
</script>

<style scoped>

</style>
